<?php
namespace Home\Controller;
use Think\Controller;
class PassportController extends BaseController {
    //登录
    public function login(){
        if (!IS_POST) {
            if(session("login_user")){
              $this->redirect('Home/Index/index');
            }
            //如果有cookie记录，则自动登录
            $cookie_token = cookie('cookie_token');
            if ($cookie_token) {
                $ret = D("User")->where("cookie_token = '%s' ",array($cookie_token))->find();
                if ($ret && $ret['cookie_token_expire'] > time() ) {
                    $login_user = $ret ;
                    session("login_user" , $login_user);
                    $this->redirect('Home/Index/index');
                }
            }
            $this->display();
        }else{
            $postdata = I('post.');
            $User = D('User');
            
            //验证用户名密码是否正确
            $ret = D("User")->checkLogin($postdata);
            if ($ret) {
              //设置cookie和session
              session("login_user" , $ret );
              //是否需要自动登录，默认记住一周
              $cookie_token = md5(time().rand().'efeffthdh');
              if($remember == "on"){
                $cookie_token_expire = time() + 60*60*24*7 ;
                cookie('cookie_token',$cookie_token,60*60*24*7);
              }
              else{
                $cookie_token_expire = 0;
                cookie('cookie_token',$cookie_token,60*30);
              }
              D("User")->where(" user_id = '$ret[user_id]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
              unset($ret['password']);
              $this->ajaxReturn(['success'=>true,'msg'=>'登录成功！','url'=>U('Home/Index/index')]);exit;
            }else{
              $this->ajaxReturn(['success'=>false,'msg'=>'用户名或密码不正确']);exit;
            }
        }
    }

    //注册
    public function register(){
        if (!IS_POST) {
            if(session("login_user")){
              $this->redirect('Home/Index/index');
            }
            $this->display();
        }else{
            $mdlUser = D("User");
            $username = I("username");
            $password = I("password");
            $confirm_password = I("confirm_pwd");

            if($password != $confirm_password){
                $this->ajaxReturn(['success'=>false,'msg'=>'注册失败！']);exit;
            }

            if(!$mdlUser->create($_POST,1)){
                $this->ajaxReturn(['success'=>false,'msg'=>$mdlUser->getError()]);exit;
            }
            else{
                $ret = $mdlUser->register($username,$password);
                $this->ajaxReturn(['success'=>true,'msg'=>'注册成功！','url'=>U('Home/Passport/login')]);exit;
            }
        }
    }

    //检测用户名是否已经存在
    public function checkUserName(){
      $username = I('username');
      $mdlUser = D("user");
      if($mdlUser -> isExist($username)){
        $this->ajaxReturn(array(
            'valid' => false,
        ));
      }
      else{
        $this->ajaxReturn(array(
            'valid' => true,
        ));
      }
    }

    //退出登录
    public function exist(){
        $login_user = $this->checkLogin();
        session("login_user" , NULL);
        cookie('cookie_token',NULL);
        $this->message("退出成功！",U('Home/Passport/login'));
    }
}